<template>
  <div>
        <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
        <!-- 搜索框和添加按钮区域 -->
         <el-row :gutter="20">
            <el-col :span='9'>
                <!-- 搜索用户,将输入框的value绑定到query上
                     并随着queryinfo对象的数据改变,重新发起请求
                     并当我们清空输入框时(clearable被触发),@clear事件被执行,重新再发送请求,获取全部数据
                 -->
            <el-input placeholder="搜索商品" clearable v-model="queryInfo.query" @clear='getGoodsList'>
                <el-button slot="append" icon="el-icon-search" @click='getGoodsList'></el-button>
            </el-input>
            </el-col>
            <el-col :span="4">
                <el-button type="primary" @click="goAddpage">添加商品</el-button>
            </el-col>
        </el-row>
            <!-- table表格区域 -->
        <el-table :data="GoodsList" border stripe>
            <!-- 索引列 -->
          <el-table-column type="index"></el-table-column>
          <el-table-column
                prop="goods_name"
                label="商品名称">
          </el-table-column>
           <el-table-column
                prop="goods_price"
                label="商品价格(元)" width="95px">
          </el-table-column>
          <el-table-column
                prop="goods_weight"
                label="商品重量" width="70px">
          </el-table-column>
           <el-table-column
                label="创建时间" width="160px">
                <template slot-scope="scope">
                    <!-- 使用自定义的全局过滤器 -->
                     {{scope.row.add_time | dateFormat}}   
                </template>
          </el-table-column>
           <el-table-column width="130px" label="操作">
             <template slot-scope="scope">
                <el-button icon="el-icon-edit" type="primary" size="mini"></el-button>
                 <el-button icon="el-icon-delete" type="danger" size="mini" @click="deleteGoods(scope.row.goods_id)"></el-button>
             </template>
          </el-table-column>
        </el-table>
        <!-- 分页区域 -->
         <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            background
            :total="total">
         </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
    data() {
        return {
        // 商品列表数据
        GoodsList:[],
        // 商品列表数据的请求参数
        queryInfo:{
            query:'', // 请求的关键字
            pagenum:1, // 页码值
            pagesize:10 // 每页显示数据条数
          },
        //   总数据条数
          total:0, 
        }
    },
    created(){
        this.getGoodsList()
    },
    methods:{
        // 根据分页获取对应的商品列表
       async getGoodsList(){
         const {data:res} = await this.$http.get(`goods`,{params:this.queryInfo})
         if(res.meta.status !==200){
             this.$message.error('获取商品列表失败！')
          }
          console.log(res.data);
          this.total = res.data.total
          this.GoodsList = res.data.goods
        },
        // 监听 每页显示的条数(pagesize) 的变化
        handleSizeChange(newValue){
            this.queryInfo.pagesize = newValue
            // 重新获取数据
            this.getGoodsList()
        },
        // 监听 当前页码值的变化
        handleCurrentChange(newValue){
             this.queryInfo.pagenum = newValue
              // 重新获取数据
             this.getGoodsList()
        },
        // 根据id删除商品
        async deleteGoods(id){
         const  confirmResult = await this.$confirm('是否删除该商品?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err=>{return err})

        if(confirmResult === 'cancel'){
            return this.$message.error('已取消删除！')
        }

        const {data:res} = await this.$http.delete(`goods/${id}`)
        if(res.meta.status !== 200){
            return this.$message.error('删除商品失败')
        }
        this.$message.success('删除商品成功')
        this.getGoodsList();
        },
        // 添加商品按钮，点击跳转添加页面
        goAddpage(){
            // 跳转到goods/add路径下
             this.$router.push({path:'goods/add'})
        }
    }
}
</script>

<style lang="less" scoped>
.el-pagination{
    margin-top: 15px;
}
</style>